<template>
  <div class="rent_main">
    <div class="text_content">
      <textarea @keyup="writeContent" @input="writeContent" name="" id="" :placeholder="holder" cols="30" rows="10" v-model="value"
                maxlength="200" @change="passValue"></textarea>
      <div class="count">
        {{currentCount}}/{{totalCount-currentCount}}
      </div>
    </div>
    <div class="vue-upload-img-multiple">
      <ol>
        <li v-show="image" v-for="(image,index) in imagesLrz" :key="index">
          <div class="img_size">
            <div @click='delImage(index)' :style="'backgroundImage:url('+image+')'" class="picture"></div>
            <span @click='delImage(index)' class="remove">x</span>
          </div>
        </li>
        <li v-show="showLoading">
          <img class="loading" src="./images/loading.gif" alt="">
        </li>
        <li class="input-container">
          <input type="file" @change="onFileChange" accept="image/gif,image/jpeg,image/jpg,image/png"
                 class="file-input">
        </li>
      </ol>
    </div>
    <div class="prompt">
      <span>（不能超过4张图片）</span>
    </div>
    <!--<button @click='addPic' style="margin-top: 40px; font-size: 20px;">上传</button>-->
  </div>
</template>
<style lang="scss">
  /*@import "../../common/sass/index.scss";*/
  /*@import "../../common/sass/base.scss";*/
  .mint-indicator-mask {
    top: 0;
    left: 0;
    position: fixed;
    width: 100%;
    height: 100%;
    opacity: 0.5;
    background: #000;
    z-index: 667;
  }
  .rent_main {
    width: 100%;
    background-color: #fff;
    .text_content {
      padding: 10px;
      position: relative;
      .mint-cell:first-child .mint-cell-wrapper {
        background-color: #f2f2f2;
        border-radius: 6px;
        .mint-field-core {
          resize: none;
          background-color: #f2f2f2;
          font-size: 14px;
        }
      }
      textarea {
        box-sizing: border-box;
        width: 100%;
        padding: 10px;
        height: 120px;
        background: #f2f2f2;
        border-radius: 5px;
        resize: none;
        outline: none;
        border: none;
      }
      .count {
        position: absolute;
        bottom: 20px;
        right: 20px;
        color: #0078de;
      }
    }
    .vue-upload-img-multiple {
      width: 100%;
      height: 100%;
      padding: 0 10px 10px;
      box-sizing: border-box;
      display: inline-block;
      ol {
        display: flex;
        flex-wrap: wrap;
        li {
          width: 25%;
          margin-top: 5px;
          text-align: center;
          .loading {
            display: block;
            margin: 0 auto;
            margin-top: 20px;
          }
          .img_size {
            display: inline-block;
            position: relative;
            .picture {
              width: 70px;
              height: 70px;
              float: left;
              border-radius: 4px;
              background-position: center center;
              background-repeat: no-repeat;
              background-size: cover;
            }
            .remove {
              position: absolute;
              top: -10px;
              right: -8px;
              width: 18px;
              height: 18px;
              z-index: 11;
              line-height: 18px;
              text-align: center;
              border-radius: 50%;
              // border-bottom: 1px solid #000;
              background-color: red;
              color: #fff;
              font-size: 15px;
            }
          }
        }
        .input-container {
          display: inline-block;
          background: url('../common/images/tianjiatupian@2x.png') no-repeat;
          width: 70px;
          height: 70px;
          background-size: 100% 100%;
          text-align: center;
        }
        .file-input {
          height: 70px;
          width: 70px;
          opacity: 0;
          font-size: 0;
        }
      }
    }
    .prompt {
      width: 100%;
      height: 30px;
      text-align: left;
      span {
        font-size: 13px;
        line-height: 30px;
        color: red;
        margin-left: 5px;
      }
    }
  }
</style>
<script>
  import { Toast } from 'mint-ui'
  import lrz from 'lrz'

  export default {
    data () {
      return {
        value: '',
        imagesLrz: [],
        showLoading: false,
        totalCount: 200,
        currentCount: 0
      }
    },
    props: ['holder'],
    created () {
//      console.log(this.holder)
    },
    methods: {
      writeContent() {
        this.currentCount = this.value.length
        let remainCount = this.totalCount - this.currentCount
        if (!remainCount) {
          Toast({
            message: '字数不能超过200',
            position: 'middle',
            duration: 1000
          })
        }
      },
      passValue() {
        this.$emit('getValue', this.value)
      },
      onFileChange (e) {
        if (this.imagesLrz.length > 3) {
          Toast({
            message: '最多上传4张图片',
            position: 'middle',
            duration: 2000
          })
          return
        }
        let files = e.target.files || e.dataTransfer.files
        if (!files.length) return
        this.createImage(files)
      },
      createImage (file, e) {
        this.showLoading = true
        lrz(file[0], {quality: 0.4})
          .then((rst) => {
            let img = new Image()
            img.src = rst.base64
            this.imagesLrz.push(rst.base64)
            this.$emit('getImage', this.imagesLrz)
            this.showLoading = false
            // return rst
          })
          .always(function () {
            // 清空文件上传控件的值
            e.target.value = null
          })
      },
      delImage (index) {
        this.imagesLrz.splice(index, 1)
      }
    }
  }
</script>
